<template>
  <div class="comment-item">
    <div class="comment-header">
      <div class="user-info">
        <img 
          :src="baseApi + comment.avatarUrl" 
          :alt="comment.username"
          class="user-avatar"
          @error="handleImageError"
        />
        <div class="user-details">
          <div class="username">{{ comment.isAnonymous ? '匿名用户' : comment.username }}</div>
          <div class="comment-time">{{ formatTime(comment.createTime) }}</div>
        </div>
      </div>
      <div class="rating">
        <el-rate 
          v-model="comment.rating" 
          disabled 
          show-score 
          text-color="#ff9900"
          score-template="{value}分"
        />
      </div>
    </div>
    
    <div class="comment-content">
      <p>{{ comment.content }}</p>
      <div v-if="comment.images" class="comment-images">
        <img 
          v-for="(image, index) in imageList" 
          :key="index"
          :src="baseApi + image"
          :alt="`评论图片${index + 1}`"
          class="comment-image"
          @click="previewImage(image)"
          @error="handleImageError"
        />
      </div>
    </div>
    
    <div v-if="comment.replyContent" class="merchant-reply">
      <div class="reply-header">
        <i class="el-icon-chat-dot-round"></i>
        <span>商家回复</span>
        <span class="reply-time">{{ formatTime(comment.replyTime) }}</span>
      </div>
      <div class="reply-content">{{ comment.replyContent }}</div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'CommentItem',
  props: {
    comment: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      baseApi: this.$store.state.baseApi,
      defaultAvatar: '/avatar/default.png'
    }
  },
  computed: {
    imageList() {
      if (!this.comment.images) return []
      return this.comment.images.split(',').filter(img => img.trim())
    }
  },
  methods: {
    formatTime(time) {
      if (!time) return ''
      const date = new Date(time)
      const now = new Date()
      const diff = now.getTime() - date.getTime()
      
      const minute = 60 * 1000
      const hour = 60 * minute
      const day = 24 * hour
      const month = 30 * day
      const year = 365 * day
      
      if (diff < minute) {
        return '刚刚'
      } else if (diff < hour) {
        return Math.floor(diff / minute) + '分钟前'
      } else if (diff < day) {
        return Math.floor(diff / hour) + '小时前'
      } else if (diff < month) {
        return Math.floor(diff / day) + '天前'
      } else if (diff < year) {
        return Math.floor(diff / month) + '个月前'
      } else {
        return Math.floor(diff / year) + '年前'
      }
    },
    
    handleImageError(event) {
      event.target.src = this.baseApi + this.defaultAvatar
    },
    
    previewImage(image) {
      // 图片预览功能
      this.$alert(`<img src="${this.baseApi + image}" style="width: 100%; max-width: 500px;" />`, '图片预览', {
        dangerouslyUseHTMLString: true,
        showConfirmButton: false,
        customClass: 'image-preview-dialog'
      })
    }
  }
}
</script>

<style scoped>
.comment-item {
  background: #ffffff;
  border-radius: 12px;
  padding: 20px;
  margin-bottom: 16px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  transition: all 0.3s ease;
}

.comment-item:hover {
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.15);
}

.comment-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 15px;
}

.user-info {
  display: flex;
  align-items: center;
}

.user-avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  margin-right: 12px;
  object-fit: cover;
  border: 2px solid #f0f0f0;
}

.user-details {
  display: flex;
  flex-direction: column;
}

.username {
  font-weight: 600;
  color: #333;
  font-size: 14px;
}

.comment-time {
  color: #999;
  font-size: 12px;
  margin-top: 2px;
}

.rating {
  display: flex;
  align-items: center;
}

.comment-content {
  margin-bottom: 15px;
}

.comment-content p {
  margin: 0;
  line-height: 1.6;
  color: #666;
  font-size: 14px;
}

.comment-images {
  display: flex;
  gap: 8px;
  margin-top: 12px;
  flex-wrap: wrap;
}

.comment-image {
  width: 80px;
  height: 80px;
  border-radius: 8px;
  object-fit: cover;
  cursor: pointer;
  transition: transform 0.2s ease;
  border: 1px solid #e0e0e0;
}

.comment-image:hover {
  transform: scale(1.05);
}

.merchant-reply {
  background: #f8f9fa;
  border-radius: 8px;
  padding: 12px;
  border-left: 4px solid #409eff;
}

.reply-header {
  display: flex;
  align-items: center;
  margin-bottom: 8px;
  font-size: 13px;
  color: #666;
}

.reply-header i {
  margin-right: 6px;
  color: #409eff;
}

.reply-time {
  margin-left: auto;
  color: #999;
}

.reply-content {
  color: #333;
  font-size: 14px;
  line-height: 1.5;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .comment-item {
    padding: 15px;
  }
  
  .comment-header {
    flex-direction: column;
    align-items: flex-start;
    gap: 10px;
  }
  
  .rating {
    align-self: flex-end;
  }
  
  .comment-images {
    gap: 6px;
  }
  
  .comment-image {
    width: 60px;
    height: 60px;
  }
}
</style>

<style>
.image-preview-dialog {
  max-width: 90vw;
}

.image-preview-dialog .el-message-box__content {
  text-align: center;
}
</style> 